package com.jackrain.nea.wms.utils;

import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.core.db.Tools;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.wms.mapper.WmsSqlMapper;

import java.sql.Timestamp;

/**
 * Created by zhoucheng on 17/12/16.
 */
public class RecLogUtils {


    /**
     * @param name
     * @param error
     * @param topic
     */
    public static void recCloudErrorLog(String name, String error, String topic) {

        try {
            WmsSqlMapper sqlMapper = ApplicationContextHandle.getBean(WmsSqlMapper.class);
            //记录日志
            JSONObject insertKeys = new JSONObject();
            insertKeys.put("id", Tools.getSequence("oms_clouderror"));
            insertKeys.put("ad_client_id", 37);
            insertKeys.put("ownerid", 893);
            insertKeys.put("modifierid", 893);
            insertKeys.put("creationdate", new Timestamp(System.currentTimeMillis()));
            insertKeys.put("modifieddate", new Timestamp(System.currentTimeMillis()));
            insertKeys.put("isactive", "Y");
            insertKeys.put("name", name);
            insertKeys.put("ptype", "1");//服务器端
            insertKeys.put("errorinfo", error);
            insertKeys.put("topic", topic);
            insertKeys.put("ip", NetUtils.getLocalAddress().getHostAddress());
            sqlMapper.insert("oms_clouderror", insertKeys);

        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    /**
     * 记录任务日志
     */
    public static long recCloudTaskLog(String isError, String topic, String tag) {

        return recCloudTaskLog("", "", isError, -1, "", topic, tag);
    }

    /**
     * 记录任务日志
     */
    public static long recCloudTaskLog(String beginDate, String endDate, String isError, int dataCount, String remark, String topic, String tag) {

        long id = 0;
        try {
            WmsSqlMapper sqlMapper = ApplicationContextHandle.getBean(WmsSqlMapper.class);
            //记录任务
            JSONObject insertKeys = new JSONObject();
            id = Tools.getSequence("oms_cloudtask_log");
            insertKeys.put("id", id);
            insertKeys.put("ad_client_id", 37);
            insertKeys.put("ownerid", 893);
            insertKeys.put("modifierid", 893);
            insertKeys.put("creationdate", new Timestamp(System.currentTimeMillis()));
            insertKeys.put("modifieddate", new Timestamp(System.currentTimeMillis()));
            insertKeys.put("isactive", "Y");
            insertKeys.put("iserror", isError);//Y是,N否,I处理中
            if (!ObjectToUtil.isNullOREmpty(beginDate)) {
                insertKeys.put("begindate", beginDate);
            }
            if (!ObjectToUtil.isNullOREmpty(endDate)) {
                insertKeys.put("enddate", endDate);
            }
            if (dataCount >= 0) {
                insertKeys.put("datacount", dataCount);
            }
            if (!ObjectToUtil.isNullOREmpty(remark)) {
                insertKeys.put("remark", remark);
            }
            insertKeys.put("topic", topic);
            insertKeys.put("tag", tag);//
            sqlMapper.insert("oms_cloudtask_log", insertKeys);

        } catch (Exception e1) {
            e1.printStackTrace();
        }

        return id;
    }

    public static void updateCloudTaskLog(long id, int dataCount) {

        updateCloudTaskLog(id, dataCount, "");
    }

    public static void updateCloudTaskLog(long id, String isError) {

        updateCloudTaskLog(id, 0, isError);
    }

    public static void updateCloudTaskLog(long id, int dataCount, String isError) {

        updateCloudTaskLog(id, dataCount, isError, "");
    }

    public static void updateCloudTaskLog(long id, int dataCount, int successCount, String isError) {

        updateCloudTaskLog(id, "", "", dataCount, successCount, isError, "");
    }

    public static void updateCloudTaskLog(long id, String isError, String remark) {

        updateCloudTaskLog(id, 0, isError, remark);
    }

    public static void updateCloudTaskLog(long id, int dataCount, String isError, String remark) {

        updateCloudTaskLog(id, "", "", dataCount, 0, isError, remark);
    }

    public static void updateCloudTaskLog(long id, int dataCount, int successCount, String isError, String remark) {

        updateCloudTaskLog(id, "", "", dataCount, successCount, isError, remark);
    }

    public static void updateCloudTaskLog(long id, String beginDate, String endDate, int dataCount, String isError, String remark) {

        updateCloudTaskLog(id, beginDate, endDate, dataCount, 0, isError, remark);
    }

    public static void updateCloudTaskLog(long id, String beginDate, String endDate, int dataCount, int successCount, String isError, String remark) {

        if (id <= 0) {
            return;
        }

        try {
            WmsSqlMapper sqlMapper = ApplicationContextHandle.getBean(WmsSqlMapper.class);
            JSONObject setKeys = new JSONObject();
            if (!ObjectToUtil.isNullOREmpty(isError)) {
                setKeys.put("iserror", isError);
            }
            if (!ObjectToUtil.isNullOREmpty(remark)) {
                setKeys.put("remark", remark);
            }
            if (!ObjectToUtil.isNullOREmpty(beginDate)) {
                setKeys.put("begindate", beginDate);
            }
            if (!ObjectToUtil.isNullOREmpty(endDate)) {
                setKeys.put("enddate", endDate);
            }
            if (dataCount > 0) {
                setKeys.put("datacount", dataCount);
            }
            if (successCount > 0) {
                setKeys.put("successcount", successCount);
            }
            JSONObject whereKeys = new JSONObject();
            whereKeys.put("id", id);
            sqlMapper.update("oms_cloudtask_log", setKeys, whereKeys);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

}